package com.star.gc4.service.impl;

//import com.github.pagehelper.PageHelper;
//import com.github.pagehelper.PageInfo;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.star.gc4.common.R;
import com.star.gc4.entity.Log;
import com.star.gc4.entity.Msongs;
import com.star.gc4.entity.Music;
import com.star.gc4.mapper.MusicMapper;
import com.star.gc4.service.LogService;
import com.star.gc4.service.MsongsService;
import com.star.gc4.service.MusicService;
import com.star.gc4.utils.ResolveLrc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class MusicServiceImpl extends ServiceImpl<MusicMapper,Music> implements MusicService {

	@Value("${music.path}")
	private String MusicPath;

	@Value("${lrc.path}")
	private String LrcPath;

	@Autowired
	private MusicMapper musicMapper;

	@Autowired
	private MsongsService msongsService;

	@Autowired
	private LogService logService;

	@Override
	public R<String> addView(String id) {
		Music music = musicMapper.selectById(id);
		music.setView(music.getView()+1);
		musicMapper.updateById(music);
		return R.success("520");
	}

	@Override
	public R<String> save1(Music music) {
		//album不能为空
		if (music.getAlbum()==null){
			music.setAlbum("");
		}
		if (music.getSongid()==null){
			if (!IsExit(music)){
				musicMapper.insert(music);
				return R.success(music.getSongid()+"");
			}
			return R.success("-1");
		}
		else {
			QueryWrapper<Music> where = new QueryWrapper<>();
			where.eq("songid",music.getSongid());
			if (!IsExit(music)){
				musicMapper.update(music,where);
				return R.success(music.getSongid()+"");
			}
			return R.success("-1");
		}
	}

	@Override
	public R<String> delete1(String[] index) {
		try {
			Music oldmusic = musicMapper.selectById(index[0]);
			Log log = new Log();
			BeanUtils.copyProperties(oldmusic,log);
			log.setSongid(null);
			log.setLogdate(LocalDateTime.now());
			logService.save(log);


			LambdaQueryWrapper<Msongs> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq(Msongs::getSongid,index[0]);
			msongsService.remove(queryWrapper);

			musicMapper.deleteBatchIds(Arrays.asList(index));
			return R.success("520");
		}
		catch (Exception e){
			return R.success("-1");
		}
	}

	@Override
	public R<String> uploadMusic1(MultipartFile music, Integer songid) throws IOException {
		log.info("存放目录: {}",MusicPath+music.getOriginalFilename());
		String uuidname = UUID.randomUUID().toString();
		music.transferTo(new File(MusicPath+uuidname));
		UpdateWrapper<Music> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("songid",songid).set("location",uuidname);
		musicMapper.update(null, updateWrapper);
		return R.success("520");
	}

	@Override
	public R<String> uploadLrc1(MultipartFile lrc, Integer songid) throws IOException {
		log.info("存放目录: {}",LrcPath+lrc.getOriginalFilename());
		String uuidname = UUID.randomUUID().toString();
		lrc.transferTo(new File(LrcPath+uuidname));
		UpdateWrapper<Music> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("songid",songid).set("lrclocation",uuidname);
		musicMapper.update(null, updateWrapper);
		return R.success("520");
	}

	@Override
	public R<Music> get1(Integer songid) {
		return R.success(musicMapper.selectById(songid));
	}

	@Override
	public R<Page<Music>> list1(String keyword,int pageSize,int currentPage) {
		Page<Music> page = new Page<>();
		page.setCurrent(currentPage);
		page.setSize(pageSize);
		QueryWrapper<Music> where = new QueryWrapper<>();
		if (keyword==null){
			keyword = "";
		}
		where.like("songname",keyword).or().like("singer",keyword);
		Page<Music> result = musicMapper.selectPage(page, where);
		return R.success(result);
	}

	@Override
	public R<Map> Lrc(String songid) throws IOException {
		Music music = musicMapper.selectById(songid);
		if (music.getLrclocation()!=null){
			return R.success(ResolveLrc.resolve(LrcPath+music.getLrclocation()));
		}
		else {
			return R.success(null);
		}
	}

	private boolean IsExit(Music music){
		boolean result = false;
		QueryWrapper<Music> where = new QueryWrapper<>();
		where.eq("singer",music.getSinger())
				.eq("songname",music.getSongname())
				.eq("album",music.getAlbum());
		if (music.getSongid()!=null){
			//如果是update状态，查找出自己之外的符合条件的数据
			where.ne("songid",music.getSongid());
		}
		Music m = musicMapper.selectOne(where);
		if (m!=null){
			result = true;
		}
		return result;
	}
}
